package rhc.taskSchedule.access;

import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import rhc.discribute.node.commander.Commander;
import rhc.discribute.node.commander.impl.CommanderImpl;
import rhc.taskSchedule.SingleInstanceComponent;
import rhc.taskSchedule.access.noticeCustom.MyNoticeCustom;
import rhc.taskSchedule.commander.TaskScheduleCommander;
import rhc.taskSchedule.commander.TaskScheduleCommanderExecute;
import rhc.taskSchedule.entity.Running;
import rhc.taskSchedule.entity.RunningStartType;
import rhc.taskSchedule.entity.RunningType;
import rhc.taskSchedule.entity.Task;
import rhc.taskSchedule.entity.TaskType;
import rhc.taskSchedule.task.ScheduleTaskInnerService;
import rhc.taskSchedule.task.ScheduleTaskTypeInnerService;
import rhc.taskSchedule.task.impl.ScheduleTaskInnerServiceImpl;
import rhc.taskSchedule.task.impl.ScheduleTaskTypeInnerServiceImpl;
import rhc.taskSchedule.taskDistribute.TaskDistribute;
import rhc.taskSchedule.util.UUIDUtil;

public class Access {

	public static final String TEST_TASK_TYPE_NAME="TEST_TASK_TYPE";
	
	public static void main(String[] args) throws Exception{
		ApplicationContext context=new ClassPathXmlApplicationContext("taskScheduleApplicationContext.xml");
		//分布式节点
		Commander distributeNodeCommander=new CommanderImpl("/config.properties");
		distributeNodeCommander.begin();
		//将分布式节点传递给分布式任务调度，
		TaskScheduleCommander commander=new TaskScheduleCommander(distributeNodeCommander);
		//将任务类型名为asdfsad的任务注册一个通知，当有该任务分配到本节点运行会触发通知
		commander.registerNoticeCustom("asdfsad", new MyNoticeCustom());
		
		Thread.sleep(TimeUnit.MINUTES.toMillis(1));
		
		ScheduleTaskInnerService taskService=SingleInstanceComponent.getInstance(ScheduleTaskInnerServiceImpl.class);
		ScheduleTaskTypeInnerService taskTypeService=SingleInstanceComponent.getInstance(ScheduleTaskTypeInnerServiceImpl.class);
		
		TaskType taskType=taskTypeService.createOrGetScheduleTaskType(TEST_TASK_TYPE_NAME, Arrays.asList("client"));
		String taskTypeId=taskType.getId();
		
		String id=UUIDUtil.randomUUID();
		Task task=taskService.createScheduleTask("asdfsda", id, 1, taskTypeId);
		
		String sourceTaskId=task.getId();	
		
		TaskScheduleCommanderExecute execute=commander.getTaskScheduleCommanderExecute();
		TaskDistribute distribute=execute.getTaskDistribute();
		Running running=distribute.createRunning(sourceTaskId, RunningType.TASK, RunningStartType.MANUAL);
		String runningId=running.getId();
		distribute.startRunning(runningId);
	}
	
}
